package com.wxs.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxs.common.dao.UserMapper;
import com.wxs.common.pojo.User;
import com.wxs.common.service.UserService;
import com.wxs.common.utils.DataGridModel;
import com.wxs.common.utils.DataResult;

/**
 * 用户
 * 
 * @author admin
 *
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public PageInfo<User> findAllUser(int pageNum, int pageSize) {
		// 将参数传给这个方法就可以实现物理分页了，非常简单。
		PageHelper.startPage(pageNum, pageSize);
		List<User> user = userMapper.selectAllUser();
		PageInfo<User> result = new PageInfo<User>(user);
		return result;
	}

	@Override
	public User selectByName(String name) {
		return userMapper.selectByName(name);
	}

	@Override
	public DataResult queryPage(String name, Boolean enabled, DataGridModel dgm) {
		PageHelper.startPage(dgm.getPage(), dgm.getLimit());
		List<User> list = userMapper.list(name, enabled);
		PageInfo<User> p = new PageInfo<>(list);
		return new DataResult(list, p.getTotal());
	}

	@Override
	public int saveAdd(User user) {
		return userMapper.insertSelective(user);
	}

	@Override
	public int saveEdit(User user) {
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Override
	public Boolean checkName(String name, Integer id) {
		User user = userMapper.selectByName(name);
		if (user == null) {
			return false;
		} else {
			if (id == null) {
				return true;
			} else if (user.getId().equals(id)) {
				return false;
			} else {
				return true;
			}
		}
	}

	@Override
	public User findById(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public int deleteById(Integer id) {
		return userMapper.deleteByPrimaryKey(id);
	}

}
